﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace ETWebApi2.Controllers
{
    public class WriteEmail
    {
        private string UserName = "系统管理员";
        /// <summary>
        ///  POP3,SMTP     
        ///  例如:smtp.qq.com||smtp.loogtream.com 
        /// </summary>
        private string EmailHost = "smtp.163.com";
        private string UserAddress = "buyitech@163.com";
        private string Password = "baxfha";
        private string Title = "测试信息通知";
        private string FilePath = "";
        //接受邮件的人
        private string ToUser = "1191024687@qq.com";
        private void IsOk()
        {
            if (string.IsNullOrEmpty(UserAddress))
            {
                throw new Exception("未输入：");
            }
        }
        /// <summary>
        /// 邮件发送  
        /// </summary>
        /// <param name="content">邮件内容</param>
        public void Send(string content)
        {
            try
            {
                IsOk();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = EmailHost;
                smtp.Credentials = new NetworkCredential(UserAddress, Password); //邮件服务器验证信息
                smtp.EnableSsl = true;
                MailMessage myMail = new MailMessage();
                myMail.From = new MailAddress(UserAddress, UserName);
                var toaddresss = ToUser.Split(',');
                foreach (var item in toaddresss)
                {
                    myMail.To.Add(new MailAddress(item));
                }
                if (!string.IsNullOrEmpty(FilePath))
                    myMail.Attachments.Add(new Attachment(FilePath));
                myMail.Subject = Title;
                myMail.SubjectEncoding = Encoding.UTF8;
                myMail.Body = content;
                myMail.BodyEncoding = Encoding.UTF8;
                myMail.IsBodyHtml = true;
                try
                {
                    smtp.Send(myMail);
                }
                catch (Exception)
                {

                    throw;
                }

            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}